﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace YJKArch.AreaStatistical.SearchLoop
{
	public partial class SetAreaInfoForm : System.Windows.Forms.Form
	{
		public SetAreaInfoForm(SetAreaInfoForm.FormType formType)
		{
			this.m_FormType = formType;
			this.InitializeComponent();
			if (this.m_FormType == SetAreaInfoForm.FormType.FT_FillArea)
			{
				this.Text = "设置总建筑面积";
				this.chkbIncludeColumn.Visible = true;
				this.tbHoomNumb.Visible = false;
				this.labelHoomNumb.Visible = false;
			}
			else
			{
				this.Text = "设置套内建筑面积";
				this.chkbIncludeColumn.Visible = false;
				this.tbHoomNumb.Visible = true;
				this.labelHoomNumb.Visible = true;
			}
			this.tbName.ImeMode = ImeMode.OnHalf;
			this.tbHoomNumb.ImeMode = ImeMode.OnHalf;
		}

		public string AreaName
		{
			get
			{
				return this.tbName.Text.Trim();
			}
		}

		public string HoomNumb
		{
			get
			{
				return this.tbHoomNumb.Text.Trim();
			}
			set
			{
				this.tbHoomNumb.Text = value;
			}
		}

		public string m_SelectRangeType
		{
			get
			{
				return this.m_selectRangeType;
			}
		}

		public bool IncludeColumn
		{
			get
			{
				return this.chkbIncludeColumn.Checked;
			}
		}

		private void SetAreaInfoForm_Load(object sender, EventArgs e)
		{
			if (this.m_FormType == SetAreaInfoForm.FormType.FT_FillArea)
			{
				this.ReadDataFillArea();
			}
			else
			{
				this.ReadDataSuiteArea();
			}
			this.KeepWarmBtn.Checked = true;
			this.m_selectRangeType = "KeepWarm";
		}

		private void SetAreaInfoForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_FormType == SetAreaInfoForm.FormType.FT_FillArea)
			{
				this.SaveDataFillArea();
				return;
			}
			this.SaveDataSuiteArea();
		}

		public void SaveDataSuiteArea()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "SetAreaInfoForm_SuiteArea.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.tbName.Text);
			streamWriter.WriteLine(this.tbHoomNumb.Text);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void ReadDataSuiteArea()
		{
			string path = Path.GetTempPath() + "SetAreaInfoForm_SuiteArea.dat";
			if (!File.Exists(path))
			{
				this.tbName.Text = "1-A";
				this.tbHoomNumb.Text = "101";
				base.StartPosition = FormStartPosition.CenterScreen;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			this.tbName.Text = streamReader.ReadLine();
			this.m_Name = this.tbName.Text;
			this.tbHoomNumb.Text = streamReader.ReadLine();
			this.m_Number = this.tbHoomNumb.Text;
			streamReader.Close();
			fileStream.Close();
		}

		private void ReadDataFillArea()
		{
			string path = Path.GetTempPath() + "SetAreaInfoForm_FillArea.dat";
			if (!File.Exists(path))
			{
				this.tbName.Text = "建筑总面积";
				base.StartPosition = FormStartPosition.CenterScreen;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			this.tbName.Text = streamReader.ReadLine();
			this.m_Name = this.tbName.Text;
			this.chkbIncludeColumn.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.KeepWarmBtn.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.StructureBtn.Checked = Convert.ToBoolean(streamReader.ReadLine());
			streamReader.Close();
			fileStream.Close();
		}

		public void SaveDataFillArea()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "SetAreaInfoForm_FillArea.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.tbName.Text);
			streamWriter.WriteLine(this.chkbIncludeColumn.Checked);
			streamWriter.WriteLine(this.KeepWarmBtn.Checked);
			streamWriter.WriteLine(this.StructureBtn.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetAreaInfoForm_MouseLeave(object sender, EventArgs e)
		{
			if (this.m_FormType == SetAreaInfoForm.FormType.FT_FillArea)
			{
				if (this.AreaName.CompareTo("") == 0)
				{
					this.tbName.Text = this.m_Name;
				}
			}
			else
			{
				if (this.AreaName.CompareTo("") == 0)
				{
					this.tbName.Text = this.m_Name;
				}
				if (this.HoomNumb.CompareTo("") == 0)
				{
					this.tbHoomNumb.Text = this.m_Number;
				}
			}
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				SetAreaInfoForm.SendMessage(AreaInfoCreator._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SetAreaInfoForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			SetAreaInfoForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			SetAreaInfoForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void tbHoomNumb_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9'))
			{
				e.Handled = true;
			}
		}

		private void tbName_Validating(object sender, CancelEventArgs e)
		{
			if (this.tbName.Text.Trim().CompareTo("") == 0)
			{
				MessageBox.Show("名称不能为空.", "盈建科提示");
				e.Cancel = true;
				return;
			}
		}

		private void tbHoomNumb_Validating(object sender, CancelEventArgs e)
		{
			if (this.tbHoomNumb.Text.Trim().CompareTo("") == 0)
			{
				MessageBox.Show("户号不能为空.", "盈建科提示");
				e.Cancel = true;
				return;
			}
		}

		private void tbName_TextChanged(object sender, EventArgs e)
		{
			if (this.tbName.Text.Trim().CompareTo("") != 0)
			{
				this.m_Name = this.tbName.Text;
			}
		}

		private void tbHoomNumb_TextChanged(object sender, EventArgs e)
		{
			if (this.tbHoomNumb.Text.Trim().CompareTo("") != 0)
			{
				this.m_Number = this.tbHoomNumb.Text;
			}
		}

		private void OkBtn_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void KeepWarmBtn_Click(object sender, EventArgs e)
		{
			this.m_selectRangeType = "KeepWarm";
		}

		private void StructureBtn_Click(object sender, EventArgs e)
		{
			this.m_selectRangeType = "Structure";
		}

		private string m_Name = "";

		private string m_Number = "";

		private string m_selectRangeType;

		private SetAreaInfoForm.FormType m_FormType = SetAreaInfoForm.FormType.FT_SuiteArea;

		public enum FormType
		{
			FT_FillArea,
			FT_SuiteArea
		}
	}
}
